﻿using System;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;

namespace LIK.WakeOnLan.Controls
{
    public class ButtonEditClickHelper
    {

        public ButtonEditClickHelper(ButtonEdit edit)
        {
            this.edit = edit;
            edit.ButtonClick += edit_ButtonClick;
            edit.Click += edit_Click;
        }

        private ButtonPressedEventArgs args;
        private readonly ButtonEdit edit;
        private bool needRaiseEvent;

        private void edit_Click(object sender, EventArgs e)
        {
            RaiseClick(null);
        }

        private void edit_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            RaiseClick(e);
        }

        private void RaiseClick(ButtonPressedEventArgs e)
        {
            args = e;
            needRaiseEvent = true;
            edit.BeginInvoke(new MethodInvoker(delegate { OnClick(); }));
        }


        public event ButtonPressedEventHandler Click;

        #region OnClick

        /// <summary>
        /// Triggers the Click event.
        /// </summary>
        public virtual void OnClick()
        {
            if (needRaiseEvent)
            {
                needRaiseEvent = false;
                if (Click != null)
                    Click(edit, args);
            }
        }

        #endregion

    }
}
